﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Hangjing.Common;
using Hangjing.SQLServerDAL;
using Hangjing.Model;
using Hangjing.DBUtility;

/// <summary>
///市场部统计
/// </summary>
public partial class qy_54tss_Admin_Sale_marketadminsum : System.Web.UI.Page
{
    /// <summary>
    /// 专员条件
    /// </summary>
    private string SqlWhere
    {
        get
        {
            object o = ViewState["SqlWhere"];
            return (o == null) ? "" : Convert.ToString(o);
        }
        set
        {
            ViewState["SqlWhere"] = value;
        }
    }

    /// <summary>
    /// 订单条件
    /// </summary>
    private string orderWhere
    {
        get
        {
            object o = ViewState["orderWhere"];
            return (o == null) ? "" : Convert.ToString(o);
        }
        set
        {
            ViewState["orderWhere"] = value;
        }
    }

    /// <summary>
    /// 排序字段
    /// </summary>
    protected string ordersort
    {
        get
        {
            object o = ViewState["ordersort"];
            return (o == null) ? "ordercount" : Convert.ToString(o);
        }
        set
        {
            ViewState["ordersort"] = value;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SqlWhere = " Permission = 10 ";

            orderWhere = "  1=1 and delflag = 0  ";
            orderWhere = new RightsTool().getOrderWhere(orderWhere);

            WebUtility.SetDDLCity(ddlcity);
            WebUtility.SetDDLGroup(ddlGpsIMEI);

            new OrderState().BindOrderState(rblstate);

            InitSqlwhere();
        }
    }

    /// <summary>
    /// 绑定指定的数据
    /// </summary>
    protected void BindData()
    {

        DateTime start1 = Convert.ToDateTime(tbStartTime.Text);
        DateTime end1 = Convert.ToDateTime(tbEndTime.Text);
        int xAxisCount = Convert.ToInt32((end1 - start1).TotalDays) + 1;

        IList<TogoInfo> shops = new EAdmin().GetListWithOrderStatistics(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, SqlWhere, orderWhere, ordersort);
        if (shops.Count > 0)
        {
            this.AspNetPager1.RecordCount = shops[0].DataID;
        }
        if (xAxisCount > 0)
        {
            foreach (var item in shops)
            {
                item.EBuilding = (item.ShopHaveMoney / xAxisCount).ToString("0.0");
                item.Tel = (item.allcount*1.0M / xAxisCount).ToString("0.0");
              
            }
        }



        string sql = orderWhere + " and EXISTS (SELECT id FROM eadmin WHERE id =dbo.Custorder.OldStatus AND " + SqlWhere+" ) ";
        CustorderInfo count = new Custorder().GetSumSendTime(sql);

        lbordercount.InnerText = count.OrderCount.ToString();
        lbordermoney.InnerText = count.OrderTotal.ToString();
        lballprice.InnerText = count.shopdiscountmoney.ToString();
        lbperorder.InnerText = (count.OrderCount * 1.0M / xAxisCount).ToString("0.0");
        lbpersendfee.InnerText = (count.shopdiscountmoney * 1.0M / xAxisCount).ToString("0.0");
        lbpersendtime.InnerText = Utils.second2MinAndSecond((count.delflag));
        lbperordertime.InnerText = Utils.second2MinAndSecond((count.UserId));
        timeoutcount.InnerText = count.deliversiteid.ToString();
        lbadvance.InnerText = count.Advance.ToString();
        lbCommission.InnerText = count.Commission.ToString();
        timeoutrat.InnerText = "";
        if (count.OrderCount > 0)
        {
            timeoutrat.InnerText = (count.deliversiteid * 1.0 / count.OrderCount * 100).ToString("0.0") + "%";
        }

        this.rptsum.DataSource = shops;
        this.rptsum.DataBind();

        AlertScript.AjaxRegisterScript("init();", UpdatePanel1);
    }

    protected void btSearch_Click(object sender, EventArgs e)
    {
        InitSqlwhere();
        BindData();
    }

    /// <summary>
    /// 时间变化搜索
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void settime_Click(object sender, EventArgs e)
    {
        DateTime dt = DateTime.Now; //当前时间
        string starttime = this.tbStartTime.Text;
        string endtime = this.tbEndTime.Text;
        Button bt = (Button)sender;
        switch (bt.ID)
        {
            case "btpre"://前一天
                if (starttime != "")
                {
                    tbStartTime.Text = Convert.ToDateTime(starttime).AddDays(-1).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                if (endtime != "")
                {
                    tbEndTime.Text = Convert.ToDateTime(endtime).AddDays(-1).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                break;
            case "btyestoday"://昨天
                tbStartTime.Text = DateTime.Now.AddDays(-1).ToShortDateString() + "";
                tbEndTime.Text = DateTime.Now.AddDays(-1).ToShortDateString() + "";

                break;
            case "tbtoday"://今天
                tbStartTime.Text = DateTime.Now.ToShortDateString() + "";
                tbEndTime.Text = DateTime.Now.ToShortDateString() + "";

                break;
            case "btnext"://后一天
                if (starttime != "")
                {
                    tbStartTime.Text = Convert.ToDateTime(starttime).AddDays(1).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                if (endtime != "")
                {
                    tbEndTime.Text = Convert.ToDateTime(endtime).AddDays(1).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                break;
            case "tbweek"://本周

                tbStartTime.Text = TimeHelper.WeekFirst().ToDate();

                tbEndTime.Text = DateTime.Now.ToShortDateString() + "";
                break;
            case "tbmonth"://本月
                tbStartTime.Text = DateTime.Now.ToString("yyyy-MM-01") + "";
                tbEndTime.Text = DateTime.Now.ToShortDateString() + "";
                break;
            case "tbyear"://本年
                tbStartTime.Text = DateTime.Now.ToString("yyyy-01-01") + "";
                tbEndTime.Text = DateTime.Now.ToShortDateString() + "";

                break;
            default:
                break;
        }
        InitSqlwhere();

        BindData();
    }


    /// <summary>
    /// 生成查询条件
    /// </summary>
    protected void InitSqlwhere()
    {
        SqlWhere = " Permission = 10 ";
        orderWhere = " 1=1 and delflag = 0 ";
        orderWhere = new RightsTool().getOrderWhere(orderWhere);
        if (this.tbStartTime.Text != "")
        {
            orderWhere += " and  [OrderDateTime] > '" + WebUtility.InputText(this.tbStartTime.Text) + "' ";
        }
        if (this.tbEndTime.Text != "")
        {
            orderWhere += " and OrderDateTime < '" + WebUtility.InputText(this.tbEndTime.Text) + " 23:59:59'";
        }
        if (cbremoveexcel.Checked == true)
        {
            orderWhere += " and fromweb <> '" + (int)OrderSource.ExcelImport + "'";
        }


        string ids = WebUtility.GetcheckStr(rblstate);
        if (ids != "")
        {
            orderWhere += " and OrderStatus  in (" + ids + ")";
        }
      

        if (ddlcity.SelectedValue != "0")
        {
            SqlWhere += " and cityid = " + ddlcity.SelectedValue + "";
        }
        if (ddlGpsIMEI.SelectedValue != "-1")
        {
            SqlWhere += " and AdminStatus = " + ddlGpsIMEI.SelectedValue + "";
        }
        if (tbinuse.Text.Trim() != "")
        {
            SqlWhere += " and RealName like '%" + WebUtility.InputText(tbinuse.Text) + "%'";

        }

        orderWhere += " and EXISTS (SELECT unid FROM points WHERE unid = dbo.Custorder.togoid and 1=1   ";
       

        if (tbshopname.Text != "")
        {
            orderWhere += " and Name like '%" + Utils.RegEsc(WebUtility.InputText(this.tbshopname.Text.Trim())) + "%'  ";
        }
        if (this.tb_Start.Text != "")
        {
            orderWhere += " and  [intime] > '" + WebUtility.InputText(this.tb_Start.Text) + "' ";
        }
        if (this.tb_End.Text != "")
        {
            orderWhere += " and intime < '" + WebUtility.InputText(this.tb_End.Text) + " 23:59:59'";
        }
        orderWhere += "  )";

    }


    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }


}
